To select TLS-specific DE genes specific to each tumor type we use results from model2. Results are then filtered for genes that are also significant in the parenchymal comparison of the tumor (excluded), if the genes were not shown to be specific to TLS maturation by showing significant differences in model1 comparisons (both_TLS). Genes only significant in the non parenchymal comparison of model2 (sig_TLS) and those significant in the TLS-spcific and parenchymal comparison of the tumor showing an opposite effect (opposite) were selected.
Code
sel_TLS <-function(res, pat, fdr){ pat_nam <-grep(pat, names(res), value = T) pat_nam <-grep("par", pat_nam, value = T) sub_res <-lapply(pat_nam, function(nam){ res_sel <- res[[nam]] |>filter(logFC >0& FDR < fdr) res_sel$gene <-rownames(res_sel)data.frame(res_sel) }) |>bind_rows(.id ="comp")}TLS_RCC <-sel_TLS(res_mod1, "RCC", fdr =0.05)TLS_RCC_gene <-unique(TLS_RCC$gene)write.csv(TLS_RCC_gene, paste0("../outs/TLS_RCC_genes_vis.csv"))TLS_LSCC <-sel_TLS(res_mod1, "LSCC", fdr =0.05)TLS_LSCC_gene <-unique(TLS_LSCC$gene)write.csv(TLS_LSCC_gene, paste0("../outs/TLS_LSCC_genes_vis.csv"))# General exclusion list# Genes sig DE in parenchymal comparison, but not sig in tumor-specific TLS maturationpar_res <- res_mod2[["par_inRCC_vs_LSCC"]] |>filter(FDR <0.05) |>data.frame()par_res$gene <-rownames(par_res)exclude <- par_res[!par_res$gene %in%c(TLS_RCC_gene, TLS_LSCC_gene),]write.csv(exclude, paste0("../outs/excluded_parenchymal_genes_vis.csv"))sel_genes <-function(comp1, comp2, FDR_t =0.1){ log_FC_sub <- log_FC_tab |> dplyr::filter(contrast %in%c(comp1, comp2)) |>select(c(logFC,FDR,gene, F,contrast)) |>pivot_wider(names_from = contrast, values_from =c(logFC, FDR, F))# new colum names logFC_comp1 <-grep(paste0("logFC_", comp1), colnames(log_FC_sub), value = T) logFC_comp2 <-grep(paste0("logFC_", comp2), colnames(log_FC_sub), value = T) FDR_comp1 <-grep(paste0("FDR_", comp1), colnames(log_FC_sub), value = T) FDR_comp2 <-grep(paste0("FDR_", comp2), colnames(log_FC_sub), value = T)# Add sig threshold log_FC_sub <- log_FC_sub |>mutate(sig =case_sig(FDR1 =!!sym(FDR_comp1), FDR2 =!!sym(FDR_comp2), com1 = comp1, com2 = comp2, FDR_t = FDR_t))# Genes to exclude log_FC_sub <- log_FC_sub |>mutate(selected =case_when(# exclude all sig in both & same sign & not in TLS list sig %in%c("both") &sign(!!sym(logFC_comp1)) ==sign(!!sym(logFC_comp2)) &!gene %in%c(TLS_LSCC_gene, TLS_RCC_gene) ~"exclude",# include sig in both & same sign & in TLS list sig %in%c("both") &sign(!!sym(logFC_comp1)) ==sign(!!sym(logFC_comp2)) & gene %in%c(TLS_LSCC_gene, TLS_RCC_gene) ~"both_TLS",# sig all sig in TLS only sig %in% comp1 ~"sig_TLS", sig %in%c("both") &sign(!!sym(logFC_comp1)) !=sign(!!sym(logFC_comp2)) ~"opposite",.default ="none" ) ) par_nam <-grep("_par_", colnames(log_FC_sub)) log_FC_sub <- log_FC_sub |>select(-par_nam) }